<?php
namespace Admin\Controller;
use Think\Controller;
class ManagemeController extends Controller {
   public function _initialize()  
    {  
		header("Content-Type: text/html; charset=utf-8");

    	if(!session('user')){
    		$message=D('User')->loginout(2);
    		$this->success($message,U('index/index'));die;
    	}
    } 
	public function index(){
		echo 'mangeme';	
	}
	public function me(){
		$this->display('Manageme/me_nav');
	} 
	/**
	 * 子账号列表
	 * @return [type] [description]
	 */
	public function manage_me(){
		$result=D("Wxauthorise")->manage_me();
		$this->assign('count',$result['count']);
		$this->assign('list',$result['list']);
		$this->assign('page',$result['show']);
		$this->display();
	} 
	/**
	 * 子账号删除 数据显示重写 2018 1 19 9: 41
	 * @return [type] [description]
	 */
	public function del(){
		$data = M('web_user');
		if($_POST){
			$del_arr=$_POST['del_arr'];
            $res=$data->delete($del_arr);
            if($res){
               $msg['code']=1;
               $msg['message']='删除成功';
               $msg['datas']=$res;
            }
            else{
               $msg['code']=2;
               $msg['message']='删除失败';
            }
		}
		else{
			$msg['code']=3;
			$msg['message']='传输格式错误';
			}
		$this->ajaxReturn($msg);
	}
	
	public function manage_me_chanpin(){
		$bm_goods = M('goods'); 		
		/***********************公共数据，条件*******************************/
		$common_where['u_pro_id']=session('user.u_pro_id');
		$common_where['u_city_id']=session('user.u_city_id');
		$common_where['a.is_show']=1;
		/************************产品类型输出******************************/
		$goods_type_data = $bm_goods
			 ->alias('a')
			 ->join('bm_goods_foundation b ON a.gx_id=b.gx_id','LEFT')
			 ->join('bm_user c ON a.u_id=c.u_id','LEFT')
			 ->join('bm_biaoqian d ON d.id=a.daleiID','LEFT')
			 ->join('bm_nick_name e ON e.g_id=a.id and e.u_id='.session('user.u_id'),'LEFT')
			 ->join('bm_goods_gx f ON f.id=a.gx_id',"LEFT")
			 ->join('bm_qudao g ON g.qudao_id=b.dtypename','LEFT')
			 ->field('b.name as bname,g.qudao_type,b.qudaoname,a.id,a.litter_name,a.Lendingfangshi,c.u_pro_id,c.u_city_id,d.name,e.nick_id,e.nick_name,f.timeda')
			 ->where($common_where)
			 ->group('d.name')
			 ->select();
		$goods_type=array();
		foreach ($goods_type_data as $key => $value) {
			if(!in_array($value['name'],$goods_type)){
				array_push($goods_type,$value['name']);
			}
		}
		$this->assign('goods_type',$goods_type);
		/*********************产品类型输出*********************************/
		/*********************公共数据，条件*********************************/
		if($_GET['search']||$_GET['qudao_type']||$_GET['chanpin_type']){
			if($_GET['chanpin_type']){
				$where['d.name']=array('like',"%{$_GET['chanpin_type']}%");
			}
			$search=$_GET['search'];
			$where['a.litter_name']=array('LIKE',"%".$search."%");
			switch ($_GET['qudao_type']) {
				case '0':
					
					break;
				case '1':
					$where['g.qudao_type']=array('LIKE',"%银行%");
					break;
				case '2':
					$where['g.qudao_type']=array('LIKE',"%消费金融%");
					break;
				case '3':
					$where['g.qudao_type']=array('LIKE',"%小额%");
					break;
				case '4':
					$where['g.qudao_type']=array('LIKE',"%其他%");
					break;
				default:
					break;
			}
			if(!$_GET['p'])$_get['p']=1;
			$data = M('user');
			
			$count = $bm_goods
				 ->alias('a')
				 ->join('bm_goods_foundation b ON a.gx_id=b.gx_id','LEFT')
				 ->join('bm_user c ON a.u_id=c.u_id','LEFT')
				 ->join('bm_biaoqian d ON d.id=a.daleiID','LEFT')
				 ->join('bm_nick_name e ON e.g_id=a.id and e.u_id='.session('user.u_id'),'LEFT')
				 ->join('bm_goods_gx f ON f.id=a.gx_id',"LEFT")
				 ->join('bm_qudao g ON g.qudao_id=b.dtypename','LEFT')
				 ->field('b.name as bname,g.qudao_type,b.qudaoname,a.id,a.litter_name,a.Lendingfangshi,c.u_pro_id,c.u_city_id,d.name,e.nick_id,e.nick_name,f.timeda')
				 ->where($where)
				 ->where($common_where)
				 ->count();
			$Page       = new \Think\Page($count,10);
			$show       = $Page->show();
			$list=$bm_goods
				 ->alias('a')
				 ->join('bm_goods_foundation b ON a.gx_id=b.gx_id','LEFT')
				 ->join('bm_user c ON a.u_id=c.u_id','LEFT')
				 ->join('bm_biaoqian d ON d.id=a.daleiID','LEFT')
				 ->join('bm_nick_name e ON e.g_id=a.id and e.u_id='.session('user.u_id'),'LEFT')
				 ->join('bm_goods_gx f ON f.id=a.gx_id',"LEFT")
				 ->join('bm_qudao g ON g.qudao_id=b.dtypename','LEFT')
				 ->field('b.name as bname,g.qudao_type,b.qudaoname,a.id,a.litter_name,a.Lendingfangshi,c.u_pro_id,c.u_city_id,d.name,e.nick_id,e.nick_name,f.timeda')
				 ->where($where)
				 ->where($common_where)
				 ->limit($Page->firstRow.','.$Page->listRows)
				 ->select();

		}
		else{
			if(!$_GET['p'])$_get['p']=1;
			$count = $bm_goods
				 ->alias('a')
				 ->join('bm_goods_foundation b ON a.gx_id=b.gx_id','LEFT')
				 ->join('bm_user c ON a.u_id=c.u_id','LEFT')
				 ->join('bm_biaoqian d ON d.id=a.daleiID','LEFT')
				 ->join('bm_nick_name e ON e.g_id=a.id and e.u_id='.session('user.u_id'),'LEFT')
				 ->join('bm_goods_gx f ON f.id=a.gx_id',"LEFT")
				 ->join('bm_qudao g ON g.qudao_id=b.dtypename','LEFT')
				 ->field('b.name as bname,g.qudao_type,b.qudaoname,a.id,a.litter_name,a.Lendingfangshi,c.u_pro_id,c.u_city_id,d.name,e.nick_id,e.nick_name,f.timeda')
				 ->where($common_where)
				 ->count();
			$Page       = new \Think\Page($count,10);
			$show       = $Page->show();
			$list=$bm_goods
				 ->alias('a')
				 ->join('bm_goods_foundation b ON a.gx_id=b.gx_id','LEFT')
				 ->join('bm_user c ON a.u_id=c.u_id','LEFT')
				 ->join('bm_biaoqian d ON d.id=a.daleiID','LEFT')
				 ->join('bm_nick_name e ON e.g_id=a.id and e.u_id='.session('user.u_id'),'LEFT')
				 ->join('bm_goods_gx f ON f.id=a.gx_id',"LEFT")
				 ->join('bm_qudao g ON g.qudao_id=b.dtypename','LEFT')
				 ->field('b.name as bname,g.qudao_type,b.qudaoname,a.id,a.litter_name,a.Lendingfangshi,c.u_pro_id,c.u_city_id,d.name,e.nick_id,e.nick_name,f.timeda')
				 ->where($common_where)
				 ->limit($Page->firstRow.','.$Page->listRows)
				 ->select();
		}
			$this->assign('count',$count);
			$this->assign('list',$list);
			$this->assign('page',$show);
		/*****************联表查询**********************************/

		$this->display();
	} 
	public function manage_me_chengjiao(){
		$this->display();
	} 

	/**
	 * ajax 生成二维码/业务员qudao_id=-1 银行职员为正数，对应渠道
	 * 	
	 * 子账号扫描二维码
	 */
	public function manage_me_qrcode(){
		$where['qudao_pro_id']=$_SESSION['user']['u_pro_id'];
		$where['qudao_city_id']=$_SESSION['user']['u_city_id'];
	 	$qudao_list = M('qudao')
	 				  ->where($where)
	 				  ->select();
		$this ->assign('list',$qudao_list);
		$this->display();
	} 

	/**
	 * 
	 * ajax请求返回业务员二维码和银行职员二维码
	 * @return [type] [description]
	 */
	public function my_ajax(){
		$code_w=D("Wxauthorise")->my_ajax($_POST['qudao_id'],2);//银行
		$code_h=D("Wxauthorise")->my_ajax($_SESSION['user']['u_id'],1);//业务

		$this->ajaxReturn(array($code_w,$code_h));
	}

	/**
	 * 渠道
	 * @return [type] [description]
	 */
	public function manage_me_qudao(){
		$result=D("Wxauthorise")->manage_me_qudao();
		$this->assign('count',$result['count']);
		$this->assign('list',$result['list']);
		$this->assign('page',$result['show']);
		$this->display();
	} 
	public function manage_me_shenqing(){
	   $u_id=session('user.u_id');
	   if($_GET['search']){
	   		$oa['apply_name']=array('LIKE','%'.$_GET['search'].'%');
	   		$oa['apply_phone']=array('LIKE','%'.$_GET['search'].'%');
	   		$oa['_logic']='or';
	   		$where['_complex']=$oa;
	   		

	   }
	   $url=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	   $post_url=explode('/',$url);
	   foreach ($post_url as $key => $value) {
	      if($value=='p'){
	      	$a=$key;
	      }
	      if($value=='search'){
	      	$b=$key;
	      }
	   }
	   if($a<$b){
			unset($_GET['p']);
	   }
	   $where1['apply_data']=array('gt','null');
	   if(!$_GET['p'])$_GET['p']=1;
       $count=M('lccdc')
				->alias('a')
				->join('bm_chat b ON a.chat_id=b.chat_id',"LEFT")
				->join('bm_search_data  c ON a.data_id=c.data_id',"LEFT")
				->join('bm_shenqing  d ON a.cd_id=d.cd_id',"LEFT")
				->join('bm_web_user_data  e ON a.c_id=e.c_id',"LEFT")
				->join('bm_web_user f ON a.web_id=f.web_id')
				->where($where)
				->where($where1)
				->where('f.p_u_id='.$u_id.' and b.chat_id >0')
				->count();
		$Page       = new \Think\Page($count,10);
		$show       = $Page->show();
       $list=M('lccdc')
			->alias('a')
			->join('bm_chat b ON a.chat_id=b.chat_id',"LEFT")
			->join('bm_search_data  c ON a.data_id=c.data_id',"LEFT")
			->join('bm_shenqing  d ON a.cd_id=d.cd_id',"LEFT")
			->join('bm_web_user_data  e ON a.c_id=e.c_id',"LEFT")
			->join('bm_web_user f ON a.web_id=f.web_id')
			->limit($Page->firstRow.','.$Page->listRows)
			->where($where)
			->where($where1)
			->where('f.p_u_id='.$u_id.' and b.chat_id >0')
			->select();
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->display();
	}
	/**
	 * 显示佣金列表
	 * @DateTime:    [2018-03-09 15:07:33]
	 */
	public function manage_me_yongjing(){
		$manage_yj=M('manage_yj');
		$where['uid']=$_SESSION['user']['u_id'];
		$list=$manage_yj->where($where)->select();
		$count=$manage_yj->where($where)->count();
		$page       = new \Think\Page($count,10);
		$show       = $page->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->display();
	} 
	/**
	 * 修改比例ajax请求
	 * @DateTime:    [2018-03-09 15:08:32]
	 */
	public function manage_me_yongjing_update(){
		if(IS_POST){
			$where['yj_id']=$_POST['yj_id'];
			$where['uid']=$_SESSION['user']['u_id'];
			$my_data=$_POST;
			$my_data['uid']=$_SESSION['user']['u_id'];
			$re=M('manage_yj')->where($where)->save($my_data);
			if($re){
				$msg['code']=1;
				$msg['message']='修改成功';
				$msg['data']=$re;
			}
			else{
				$msg['code']=2;
				$msg['message']='修改失败';
				$msg['data']=$re;
			}
		}
		else{
			$msg['code']=3;
			$msg['message']='请求方式错误';
		}
		$this->ajaxReturn($msg);
	}
	/**
	 * 添加比例ajax请求
	 * @DateTime:    [2018-03-09 15:19:22]
	 */
	public function manage_me_yongjing_add(){
		if(IS_POST){
			$data['uid']=$_SESSION['user']['u_id'];
			$re=M('manage_yj')->add($data);
			if($re){
				$msg['code']=1;
				$msg['message']='添加成功';
				$msg['data']=$re;
			}
			else{
				$msg['code']=2;
				$msg['message']='添加失败';
				$msg['data']=$re;
			}
		}
		else{
			$msg['code']=3;
			$msg['message']='请求方式错误';
		}
		$this->ajaxReturn($msg);
	}
	/**
	 * 删除比例ajax请求
	 * @DateTime:    [2018-03-09 15:19:22]
	 */
	public function manage_me_yongjing_del(){
		if(IS_POST){
			$where['yj_id']=$_POST['yj_id'];
			$where['uid']=$_SESSION['user']['u_id'];
			$re=M('manage_yj')->where($where)->delete();
			if($re){
				$msg['code']=1;
				$msg['message']='删除成功';
				$msg['data']=$re;
			}
			else{
				$msg['code']=2;
				$msg['message']='删除失败';
				$msg['data']=$re;
			}
		}
		else{
			$msg['code']=3;
			$msg['message']='请求方式错误';
		}
		$this->ajaxReturn($msg);
	}
	/**
	 * 添加商品别名不存在的话
	 */
	public function add_goods_nick_name(){
		if(IS_AJAX&&IS_POST){
			if(isset($_POST['nick_name'])&&$_POST['g_id']){
				$arr=$_POST;
				$arr['u_id']=session('user.u_id');
				$arr['time']=date('Y年m月d日 h:i:s');
				$res=M('nick_name')->data($arr)->add();
				if($res){
					$msg['code']=1;
					$msg['message']='添加成功';
					$msg['nick_id']=$res;
				}
				else{
					$msg['code']=2;
					$msg['message']='添加失败';
				}
			}
			else{
				$msg['code']=3;
				$msg['message']='数据不完整';
			}
		}
		else{
			$msg['code']=4;
			$msg['message']='数据格式错误';
		}
		$this->ajaxReturn($msg);
	}
	/**
	 * 修改商品别名
	 */
	public function update_goods_nick_name(){
		if(IS_AJAX&&IS_POST){
			if($_POST['nick_id']&&isset($_POST['nick_name'])){
				$arr=$_POST;
				$arr['time']=date('Y年m月d日 h:i:s');
				$res=M('nick_name')->save($arr);
				if($res){
				$msg['code']=1;
				$msg['message']='修改成功';
				$msg['data']=$res;
				}
				else{
				$msg['code']=2;
				$msg['message']='修改失败';	
				}
			}
			else{
				$msg['code']=3;
				$msg['message']='数据不完整';
			}
		}
		else{
			$msg['code']=4;
			$msg['message']='数据格式错误';
		}
		$this->ajaxReturn($msg);
	}
	
}
